package com.hss01248.dialog;


import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.comp.ProgressDialog;
import com.hss01248.dialog.config.ConfigBean;
import com.hss01248.dialog.config.DefaultConfig;
import com.hss01248.dialog.interfaces.MyDialogListener;
import com.hss01248.dialog.interfaces.MyItemDialogListener;
import com.hss01248.dialog.ios.IosAlertDialogHolder;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.List;

/**
 * Created by Administrator on 2016/5/4 0004.
 */
public class StyledDialog {


    public static Context context;

    private static boolean isMiUi8 = false;//miui8用非activity的Context时,无法以TYPE_TOAST的形式弹出对话框.没有好的解决办法.....

    public static EventHandler getMainHandler() {
        if (mainHandler == null) {
            mainHandler = new EventHandler(EventRunner.getMainEventRunner());
        }
        return mainHandler;
    }

    private static EventHandler mainHandler;

    public static void init(Context context) {
        StyledDialog.context = context;
        mainHandler = new EventHandler(EventRunner.getMainEventRunner());
        DefaultConfig.initBtnTxt(context);

    }




    /**
     * 一键让loading消失.
     */
    public static void dismissLoading(Ability activity) {
        DialogsMaintainer.dismissLoading(activity);
    }


    public static void dismiss(CommonDialog... dialogs) {
        if (dialogs != null && dialogs.length > 0) {
            for (CommonDialog dialog : dialogs) {
                try {
                    dialog.destroy();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }


    public static ConfigBean buildProgress(CharSequence msg, boolean isHorizontal) {
        return DialogAssigner.getInstance().assignProgress(context, msg, isHorizontal);
    }

    public static void updateLoadingMsg(final String msg, final CommonDialog dialog) {
        if (dialog == null) {
            return;
        }
        if (!dialog.isShowing()) {
            return;
        }
        getMainHandler().postTask(new Runnable() {
            @Override
            public void run() {
                try {
                    Component view = dialog.getContentCustomComponent().findComponentById(ResourceTable.Id_loading_msg);
                    if (view instanceof Text) {
                        Text textView = (Text) view;
                        textView.setText(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });


    }

    /**
     * 可以在任何线程调用
     *
     * @param dialog       传入show方法返回的对象
     * @param progress
     * @param max
     * @param msg          如果是转圈圈,会将msg变成msg:78%的形式.如果是水平,msg不起作用
     * @param isHorizontal 是水平线状,还是转圈圈
     */
    public static void updateProgress(final CommonDialog dialog, final int progress, final int max, final CharSequence msg, final boolean isHorizontal) {

        getMainHandler().postTask(new Runnable() {
            @Override
            public void run() {
                ProgressDialog pDialog = (ProgressDialog)dialog;
                pDialog.setProgress(progress);
                if (isHorizontal) {
                    pDialog.setMax(max);
                }
                else {
                    String pmsg = new StringBuilder(msg).append(":").append(progress * 100 / max).append("%").toString();
                    pDialog.setContentText(pmsg);
                }
            }
        });


    }

    public static ConfigBean buildLoading(CharSequence msg) {
        return DialogAssigner.getInstance().assignLoading(context, msg, true, false);
    }

    public static ConfigBean buildLoading() {
        return DialogAssigner.getInstance().assignLoading(context, DefaultConfig.loadingTxt, true, false);
    }

    public static ConfigBean buildMdLoading() {
        return DialogAssigner.getInstance().assignMdLoading(context, DefaultConfig.loadingTxt, true, false);
    }

    public static ConfigBean buildMdLoading(CharSequence msg) {
        return DialogAssigner.getInstance().assignMdLoading(context, msg, true, false);
    }


    public static ConfigBean buildMdAlert(CharSequence title, CharSequence msg, MyDialogListener listener) {
        return DialogAssigner.getInstance().assignMdAlert(context, title, msg, listener);
    }


    public static ConfigBean buildMdSingleChoose(CharSequence title, int defaultChosen, String[] words, MyItemDialogListener listener) {
        return DialogAssigner.getInstance().assignMdSingleChoose(context, title, defaultChosen, words, listener);
    }

    @Deprecated
    public static ConfigBean buildMdMultiChoose(CharSequence title, String[] words, boolean[] checkedItems, MyDialogListener btnListener) {
        return DialogAssigner.getInstance().assignMdMultiChoose(context, title, words, checkedItems, btnListener);
    }

    public static ConfigBean buildMdMultiChoose(CharSequence title, String[] words, List<Integer> selectedIndexs, MyDialogListener btnListener) {
        return DialogAssigner.getInstance().assignMdMultiChoose(context, title, words, selectedIndexs, btnListener);
    }


    public static ConfigBean buildIosAlert(CharSequence title, CharSequence msg, MyDialogListener listener) {
        return DialogAssigner.getInstance().assignIosAlert(context, title, msg, listener);
    }


    public static ConfigBean buildIosAlertVertical(CharSequence title, CharSequence msg, MyDialogListener listener) {
        return DialogAssigner.getInstance().assignIosAlertVertical(context, title, msg, listener);
    }


    public static ConfigBean buildIosSingleChoose(List<? extends CharSequence> words, MyItemDialogListener listener) {
        return DialogAssigner.getInstance().assignIosSingleChoose(context, words, listener);
    }


    public static ConfigBean buildBottomItemDialog(List<? extends CharSequence> words, CharSequence bottomTxt, MyItemDialogListener listener) {
        return DialogAssigner.getInstance().assignBottomItemDialog(context, words, bottomTxt, listener);
    }


    public static ConfigBean buildNormalInput(CharSequence title, CharSequence hint1, CharSequence hint2,
                                              CharSequence inputText1, CharSequence inputText2, MyDialogListener listener) {
        return DialogAssigner.getInstance().assignNormalInput(context, title, hint1, hint2, inputText1, inputText2, listener);
    }

    public static ConfigBean buildMdInput(CharSequence title, CharSequence hint1, CharSequence hint2,
                                          CharSequence inputText1, CharSequence inputText2, MyDialogListener listener) {
        return DialogAssigner.getInstance().buildMdInput(title, hint1, hint2, inputText1, inputText2, listener);

    }


    /**
     * 将view塞到meterial alert dialog 中, 可以沿用其title和button的样式和更改方法,该view作为content显示
     *
     * @param customViewHolder
     * @return
     */
    public static ConfigBean buildCustomInMd(SuperLvHolder customViewHolder, MyDialogListener listener) {
        return DialogAssigner.getInstance().buildCustomInMd(customViewHolder, listener);
    }


    /**
     * 将view塞到ios样式的 dialog 中, 可以沿用其title和button的样式和更改方法,该view作为content显示
     *
     * @param customViewHolder
     * @return
     */
    public static ConfigBean buildCustomInIos(SuperLvHolder customViewHolder, MyDialogListener listener) {
        return DialogAssigner.getInstance().buildCustomInIos(customViewHolder, listener);
    }

    /**
     * use buildCustom( SuperLvHolder viewHolder)  instead
     * 建议使用SuperLvHolder 对自定义view进行包装,如果有输入框,则实现控制软键盘的显示和隐藏方法,框架会在合适的时间帮你显示和隐藏
     *
     * @param contentView
     * @param gravity
     * @return
     */
    @Deprecated
    public static ConfigBean buildCustom(Component contentView, int gravity) {
        return DialogAssigner.getInstance().assignCustom(context, contentView, gravity);
    }

    /**
     * 带x的view,x的位置可以配置
     * @param contentView
     * @return
     */
    public static ConfigBean buildCustomAsAdStyle(Component contentView,  int xGravity) {
        ConfigBean configBean =  DialogAssigner.getInstance().assignCustom(context, contentView, LayoutAlignment.CENTER);
        configBean.asAdXStyle = true;
        configBean.xGravity = xGravity;
        configBean.useTheShadowBg = false;
        return configBean;
    }

    public static ConfigBean buildAlertAsAdStyle(String title,String msg,  int xGravity) {
        IosAlertDialogHolder dialogHolder = new IosAlertDialogHolder(context);
        dialogHolder.showOnlyTitleAndMsg(title,msg);
        ConfigBean configBean =  DialogAssigner.getInstance().assignCustom(context, dialogHolder.rootView, LayoutAlignment.CENTER);
        configBean.hint1 = "";
        configBean.hint2 = "";
        configBean.text1 = "";
        configBean.text2 = "";
        configBean.text3 = "";
       // dialogHolder.assingDatasAndEvents(ActivityStackManager.getInstance().getTopActivity(),configBean);
        configBean.asAdXStyle = true;
        configBean.xGravity = xGravity;
        configBean.useTheShadowBg = false;
        return configBean;
    }


    public static ConfigBean buildCustom(SuperLvHolder viewHolder) {
        return DialogAssigner.getInstance().assignCustom(null, viewHolder);
    }


    public static ConfigBean buildCustomBottomSheet(Component contentView) {
        return DialogAssigner.getInstance().assignCustomBottomSheet(context, contentView);
    }

    public static ConfigBean buildBottomSheetLv(CharSequence title, List datas, CharSequence bottomTxt, MyItemDialogListener listener) {
        return DialogAssigner.getInstance().assignBottomSheetLv(context, title, datas, bottomTxt, listener);
    }

    public static ConfigBean buildBottomSheetGv(CharSequence title, List datas, CharSequence bottomTxt, int columnsNum, MyItemDialogListener listener) {
        return DialogAssigner.getInstance().assignBottomSheetGv(context, title, datas, bottomTxt, columnsNum, listener);
    }

}
